let app = getApp();
import { getCorner, swiper } from "../../request/api";
import dayjs from "../../common/js/dayjs.js";
let iconUrl = 'https://atush.vip/static/icons/';

Component({
    /**
     * 组件的属性列表
     */
    properties: {
        freshIsPulldown: {
            type: Boolean,
            value: false,
            observer: "onPullDownMethod"
        },

        onReachBottomCount: {
            type: Number,
            value: 1,
            observer: "onBottomMethod",
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        isPageNo: false,
        lang: null,
        pageNo: 1,
        fresh: [],
        swiper_list: [],
        iconUrl: iconUrl,
        tabs: [
            { id: 1, name: 'home', icon: '/icon/home.png', fill_icon: '/icon/home-fill.png' },
            { id: 2, name: 'infCircle', icon: '/icon/circle.png', fill_icon: '/icon/circle-fill.png' },
            { id: 3, name: 'userCentr', icon: '/icon/man.png', fill_icon: '/icon/man-fill.png' }
        ],
        active: 1
    },

    /**
     * 组件的方法列表
     */
    methods: {
        // 获取首页内容
        getPost() {
            let status = 1
            let pageNo = this.data.pageNo
            let pageSize = 10
            let data = { status, pageNo, pageSize }
            getCorner(res => {
                if (res.statusCode == 200) {
                    for (let item of res.data.data) {
                        item.releaseTime = dayjs(item.releaseTime).format("YYYY-MM-DD")
                    }
                    this.setData({ fresh: this.data.fresh.concat(res.data.data), totalPage: res.data.totalPage })
                }
            }, data);

            // get swiper
            let swiper_location = 1
            data = { swiper_location }
            swiper(res => {
                this.setData({swiper_list: res.data})
            }, data)
        },
        onPullDownMethod(value) {
            console.log(value);
            if (value == true) {
                this.noMoreMethod(true)
            }
        },
        onBottomMethod(number) {
            // console.log("load more: ", number, this.data.pageNo, this.data.totalPage);
            if (this.data.pageNo >= this.data.totalPage || this.data.totalPage == undefined) {
                // console.log("没有更多啦");
                return
            }
            this.setData({ pageNo: this.data.pageNo += 1})
            this.getPost()
        },

        // 往详细页面
        onClickDetailed(item) {
            let type = item.currentTarget.dataset.item.type
            let itemId = item.currentTarget.dataset.item.target_id
            switch (type) {
                case 3:
                    wx.navigateTo({
                        url: '/pages/home/house/houseDetaild/houseDetaild?itemId=' + itemId
                    });
                    break;
                case 4:
                    wx.navigateTo({
                        url: '/pages/home/jobs/jobsDetailed/jobsDetailed?itemId=' + itemId
                    });
                    break;
                case 5:
                    wx.navigateTo({
                        url: '/pages/home/car/carDetailed/carDetailed?itemId=' + itemId
                    });
                    break;
                case 6:
                    wx.navigateTo({
                        url: '/pages/home/secondHand/secondDetailed/secondDetailed?itemId=' + itemId
                    });
                    break;
                case 7:
                    wx.navigateTo({
                        url: '/pages/home/pickedUp/pikedUpDetailed/pikedUpDetailed?itemId=' + itemId
                    });
                    break;
                case 8:
                    wx.navigateTo({
                        url: '/pages/home/throw/throwDetaild/throwDetaild?itemId=' + itemId
                    });
                    break;
                case 9:
                    wx.navigateTo({
                        url: '/pages/home/tutor/tutorDetailed/tutorDetailed?itemId=' + itemId
                    });
                    break;
                case 10:
                    wx.navigateTo({
                        url: '/pages/home/maintenance/maintenanceDetailed/maintenanceDetailed?itemId=' + itemId
                    });
                    break;
                case 13:
                    wx.navigateTo({
                        url: '/pages/home/shop/shopDetailed/shopDetailed?itemId=' + itemId
                    });
                    break;
                case 14:
                    wx.navigateTo({
                        url: '/pages/home/buyAndSell/buyAndSellDetaile/buyAndSellDetaile?itemId=' + itemId
                    });
                    break;
                case 15:
                    wx.navigateTo({
                        url: '/pages/detail/sendInfo/senInfoDetaild/senInfoDetaild?itemId=' + itemId
                    });
                    break;
                case 16:
                    wx.navigateTo({
                        url: '/pages/home/price/priceDetailed/priceDetailed?itemId=' + itemId
                    });
                    break;
                default:
                    break;
            }
        },

        // 轮播图往详细页面
        onClickSwiper(item) {
            let freshPost = JSON.stringify(item.currentTarget.dataset.item)
            let linkType = item.currentTarget.dataset.item.linkType
            let itemId = item.currentTarget.dataset.item.content_id
            let type = item.currentTarget.dataset.item.target_id

            if(linkType == 3) {
                switch (type) {
                    case 3:
                        wx.navigateTo({
                            url: '/pages/home/house/houseDetaild/houseDetaild?itemId=' + itemId
                        });
                        break;
                    case 4:
                        wx.navigateTo({
                            url: '/pages/home/jobs/jobsDetailed/jobsDetailed?itemId=' + itemId
                        });
                        break;
                    case 5:
                        wx.navigateTo({
                            url: '/pages/home/car/carDetailed/carDetailed?itemId=' + itemId
                        });
                        break;
                    case 6:
                        wx.navigateTo({
                            url: '/pages/home/secondHand/secondDetailed/secondDetailed?itemId=' + itemId
                        });
                        break;
                    case 10:
                        wx.navigateTo({
                            url: '/pages/home/maintenance/maintenanceDetailed/maintenanceDetailed?itemId=' + itemId
                        });
                        break;
                    case 13:
                        wx.navigateTo({
                            url: '/pages/home/shop/shopDetailed/shopDetailed?itemId=' + itemId
                        });
                        break;
                    case 14:
                        wx.navigateTo({
                            url: '/pages/home/buyAndSell/buyAndSellDetaile/buyAndSellDetaile?itemId=' + itemId
                        });
                        break;
                    case 15:
                        wx.navigateTo({
                            url: '/pages/detail/sendInfo/senInfoDetaild/senInfoDetaild?itemId=' + itemId
                        });
                        break;
                    default:
                        break;
                }
            }
            if (linkType == 2 || linkType == 1) {
                wx.navigateTo({
                    url: '/pages/home/published/published?freshPost=' + freshPost
                });
            }
        },

        onChangeTab(e) {
            let item, index
            item = e.detail.item
            index = e.detail.index
            this.setData({ active: index })
            if (index == 0) {
                this.setData({ navigation: true })
            } else if (index == 1) {
                this.setData({ navigation: true })
            } else if (index == 2) {
                this.setData({ navigation: false })
            }
        }
    },
    lifetimes: {
        attached: function () {
            this.setData({ lang: app.globalData.lang, navTitle: "infCircle", navigation: true })
        },
        ready(){
            this.setData({isPageNo : true})
            this.getPost()
        },
    }
})
